package com.danskin.wechat.webapi;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.danskin.wechat.entity.CustOrderRecord;
import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.service.CustOrderRecordService;
import com.danskin.wechat.util.ConvertUtils;
import com.danskin.wechat.util.DateUtils;
import com.danskin.wechat.util.Page;
import com.danskin.wechat.vo.CustOrderRecordVo;

@RestController
@RequestMapping("api/order")
public class CustOrderController extends BaseApiController {
	@Autowired
	private CustOrderRecordService orderRecordService;

	@RequestMapping(value = "getCustOrderRecord/{appId}")
	public Object getCustOrderRecord(@RequestBody Map<String, Object> map, @PathVariable("appId") String appId,
			HttpServletRequest request) throws Exception {

		if (false == this.isValid("pageIndex", map)) {
			return this.getError("pageIndex不能为空");
		} else if (false == this.isValid("openId", map)) {
			return this.getError("openId不能为空");
		}

		String sPageIndex = map.get("pageIndex").toString();

		int pageIndex = ConvertUtils.getInt(sPageIndex);
		int pageSize = 10;
		map.put("appId", appId);
		Page page = new Page();
		page.setCurrentResult(pageIndex);
		page.setShowCount(pageSize);
		page.setMap(map);

		JSONArray array = new JSONArray();
		List<CustOrderRecord> list = orderRecordService.listPageCustOrderRecordByOpenId(page);
		for (CustOrderRecord record : list) {
			JSONObject json = new JSONObject();
			json.put("id", record.getId());
			json.put("orderNo", record.getOrderNo());
			json.put("payAmount", record.getPayAmount());
			json.put("getPoints", record.getGetPoints());
			json.put("transTime", DateUtils.formatTime(record.getTransTime()));
			json.put("storeCode", record.getStoreCode());
			json.put("evaluateStatus", record.getEvaluateStatus());
			array.add(json);
		}
		return new AccessResponse<Object>("0", "ok", array);
	}

	@RequestMapping(value = "getCustOrderDetail/{appId}")
	public Object getCustOrderDetail(@RequestBody Map<String, Object> map, @PathVariable("appId") String appId,
			HttpServletRequest request) throws Exception {

		if (false == this.isValid("openId", map)) {
			return this.getError("openId不能为空");
		} else if (false == this.isValid("orderId", map)) {
			return this.getError("orderId不能为空");
		}

		String openId = map.get("openId").toString();
		String sOrderId = map.get("orderId").toString();
		int orderId = ConvertUtils.getInt(sOrderId);
		
		CustOrderRecordVo orderRecordVo = orderRecordService.queryCustOrderRecordVo(orderId, openId);
		
		return new AccessResponse<Object>("0", "ok", orderRecordVo);
	}
}
